package cn.tj212.inmall.controller.backend;

import cn.tj212.inmall.common.Const;
import cn.tj212.inmall.common.ResponseCode;
import cn.tj212.inmall.common.ServerResponse;
import cn.tj212.inmall.pojo.User;
import cn.tj212.inmall.service.ICategoryService;
import cn.tj212.inmall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/manage/category/")
public class CategoryManageController {

    @Autowired
    private IUserService userService;

    @Autowired
    private ICategoryService categoryService;

    @RequestMapping("add_category")
    @ResponseBody
    public ServerResponse addCategory(HttpSession session,String categoryName,
                                        @RequestParam(value = "parentId",defaultValue = "0") Integer parentId){
        User user= (User) session.getAttribute(Const.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorCodeMsg(ResponseCode.NEED_LOGIN.getCode(),"Please sign in!");
        }
        //校验一下是否是管理员
        if (userService.checkAdminRole(user).isSuccess()){
            //是管理员
            //增加分类逻辑
            return categoryService.addCategory(categoryName,parentId);
        }else {
            return ServerResponse.createByErrorMsg("The operation must be authorized");
        }
    }

    @RequestMapping("set_category_name")
    @ResponseBody
    public ServerResponse setCategoryName(HttpSession session,Integer categoryId,String categoryName){
        User user= (User) session.getAttribute(Const.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorCodeMsg(ResponseCode.NEED_LOGIN.getCode(),"Please sign in!");
        }
        //校验一下是否是管理员
        if (userService.checkAdminRole(user).isSuccess()){
            //更新categoryname
            return categoryService.updateCategoryName(categoryId,categoryName);
        }else {
            return ServerResponse.createByErrorMsg("The operation must be authorized");
        }
    }

    @RequestMapping("get_category")
    @ResponseBody
    public ServerResponse getChildrenParallelCategory(HttpSession session,@RequestParam(value = "categoryId",defaultValue = "0") Integer categoryId){
        User user= (User) session.getAttribute(Const.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorCodeMsg(ResponseCode.NEED_LOGIN.getCode(),"Please sign in!");
        }
        //校验一下是否是管理员
        if (userService.checkAdminRole(user).isSuccess()){
            return categoryService.getChildrenParallelCategory(categoryId);
        }else {
            return ServerResponse.createByErrorMsg("The operation must be authorized");
        }
    }

    @RequestMapping("get_deep_category.do")
    @ResponseBody
    public ServerResponse getCategoryAndDeepChildrenCategory(HttpSession session,
                                                             @RequestParam(value = "categoryId",defaultValue = "0")Integer categoryId){
        User user= (User) session.getAttribute(Const.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorCodeMsg(ResponseCode.NEED_LOGIN.getCode(),"请先登录再进行操作！");
        }
        if (userService.checkAdminRole(user).isSuccess()){
            //查询当前节点和递归子节点的ID
            return categoryService.selectCategoryAndChildrenById(categoryId);
        }else {
            return ServerResponse.createByErrorMsg("没有操作权限！");
        }
    }
}
